package com.zh.note.huawei.onehundred.关联子串;

/**
 * @Author: zhanghuan
 * @date: 2023/9/3 15:48
 * @description: create it
 */

import java.util.*;

public class Main {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            String str = "abc efghicabiii";
            String[] s = str.split(" ");
            String str1 = s[0];
            String str2 = s[1];
            char[] nums = str1.toCharArray();
            List<List<Character>> res = new ArrayList<>();
            //求str1的全排列
            Deque<Character> path = new LinkedList<>();
            boolean[] used = new boolean[nums.length];
            dfs(nums, 0, used, path, res);
            System.out.println(res);
            List<String> lists = new ArrayList<>();
            for (List<Character> re : res) {
                StringBuilder sb = new StringBuilder();
                for (Character character : re) {
                    sb.append(character);
                }
                lists.add(sb.toString());
            }
            for (String item : lists) {
                int index = str2.indexOf(item);
                if (index != -1) {
                    System.out.println(index);
                    return;
                }
            }
            System.out.println(-1);


        }
    }

    /**
     * 求全排列
     *
     * @param nums
     * @param index
     * @param used
     * @param path
     * @param res
     */
    private static void dfs(char[] nums, int index, boolean[] used, Deque<Character> path, List<List<Character>> res) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
        }
        for (int i = 0; i < nums.length; i++) {

            if (used[i]) {
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            dfs(nums, i + 1, used, path, res);
            used[i] = false;
            path.removeLast();
        }
    }

}
